/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-07 13:44:19  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.service.impl;

import java.util.Date;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.entity.BpEmergencyContingencyPlan;
import com.joysuch.wwyt.bp.entity.BpEmergencyContingencyPlanArchive;
import com.joysuch.wwyt.bp.entity.BpEmergencyContingencyPlans;
import com.joysuch.wwyt.bp.enums.RegulationDocumentTypes;
import com.joysuch.wwyt.bp.repository.BpEmergencyContingencyPlanArchiveDao;
import com.joysuch.wwyt.bp.repository.BpEmergencyContingencyPlanDao;
import com.joysuch.wwyt.bp.repository.BpEmergencyContingencyPlansDao;
import com.joysuch.wwyt.bp.service.BpEmergencyContingencyPlanArchiveService;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpEmergencyContingencyPlanArchiveServiceImpl implements BpEmergencyContingencyPlanArchiveService {

    @Autowired
    private BpEmergencyContingencyPlanArchiveDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Autowired
    private BpEmergencyContingencyPlanDao bpEmergencyContingencyPlanDao;

    @Autowired
    private BpEmergencyContingencyPlansDao bpEmergencyContingencyPlansDao;

    @Override
    public void delete(Long id) {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
    
     */
    private void checkBussinessRestrictions(Long id) {

    }

    @Override
    public BpEmergencyContingencyPlanArchive findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public void archive(Long id) {
        // 获取待归档（重新编制）的预案对象（原预案）
        BpEmergencyContingencyPlan bpEmergencyContingencyPlan = bpEmergencyContingencyPlanDao.findById(id).get();
        // 将原预案的信息添加到预案归档表中
        BpEmergencyContingencyPlanArchive content = new BpEmergencyContingencyPlanArchive();
        // 原预案ID
        content.setPlanId(bpEmergencyContingencyPlan.getId());
        // 预案名称
        content.setName(bpEmergencyContingencyPlan.getName());
        // 预案编号
        content.setCode(bpEmergencyContingencyPlan.getCode());
        // 编制部门ID
        content.setMakeDepartId(bpEmergencyContingencyPlan.getMakeDepartId());
        // 编制人
        content.setMakeUserId(bpEmergencyContingencyPlan.getMakeUserId());
        // 发布时间
        content.setMakeTimeStart(bpEmergencyContingencyPlan.getMakeTimeStart());
        content.setMakeTimeEnd(bpEmergencyContingencyPlan.getMakeTimeEnd());
        // 适用范围
        content.setApplicationScope(bpEmergencyContingencyPlan.getApplicationScope());
        // 事故风险分析
        content.setAccidentRiskAnalysis(bpEmergencyContingencyPlan.getAccidentRiskAnalysis());
        // 应急处置措施
        content.setEmergencyMeasures(bpEmergencyContingencyPlan.getEmergencyMeasures());
        // 附件ID
        content.setAttachmentId(bpEmergencyContingencyPlan.getAttachmentId());
        // 状态
        content.setState(bpEmergencyContingencyPlan.getState());
        // 审核人
        content.setAuditUserId(bpEmergencyContingencyPlan.getAuditUserId());
        // 审核时间
        content.setAuditTime(bpEmergencyContingencyPlan.getAuditTime());
        // 审核备注
        content.setAuditRemark(bpEmergencyContingencyPlan.getAuditRemark());
        // 演练级别
        content.setActLevel(bpEmergencyContingencyPlan.getActLevel());
        // 演练方式
        content.setActMethod(bpEmergencyContingencyPlan.getActMethod());
        // 归档时间
        content.setArchiveTime(new Date());
        dao.save(content);
        // 重置原文档的状态
        bpEmergencyContingencyPlan.setState(RegulationDocumentTypes.TO_BE_AUDITED.getId());
        bpEmergencyContingencyPlan.setAuditUserId(null);
        bpEmergencyContingencyPlan.setAuditTime(null);
        bpEmergencyContingencyPlan.setAuditRemark(null);
        bpEmergencyContingencyPlanDao.save(bpEmergencyContingencyPlan);

    }

}
